package com.consumer.service.impl;


import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.extra.pinyin.PinyinUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.consumer.common.MinioUtil;
import com.consumer.config.URLConfig;
import com.consumer.entity.*;
import com.consumer.service.DevopsBucketFileService;
import com.consumer.service.DevopsBucketService;
import com.consumer.service.DevopsDemandService;
import com.consumer.service.DevopsTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional(rollbackFor = Exception.class)
public class MigrationService {
    @Resource
    DevopsBucketService devopsBucketService;

    @Resource
    MinioUtil minioUtil;
    @Resource
    DevopsBucketFileService devopsBucketFileService;
    @Resource
    DevopsDemandService devopsDemandService;
    @Resource
    DevopsTaskService devopsTaskService;

    public void migrateDemand(String projectId) {

        //查询所有计划
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProjectPlan", paramMap4);
        List<TPlane> planeList = JSON.parseArray(plans, TPlane.class);

        //有多个主目标的项目
        Map<String, List<TPlane>> collect = planeList.stream().collect(Collectors.groupingBy(TPlane::getSystemId));
        List<TPlane> newPlaneList = new ArrayList<>();
        Set<String> removeSet = new HashSet();
        collect.forEach((k, v) -> {
            if (v.size() == 1) {
                newPlaneList.addAll(v);
            } else {
                removeSet.add(k);
            }
        });
        if (removeSet.contains(projectId)) {
            System.out.println("项目存在多个主目标::" + projectId);
            return;
        }
//        Map<String, String> planMap = newPlaneList.stream().collect(Collectors.toMap(TPlane::getSystemId, TPlane::getPlaneId));
        //查询所有用户
        HashMap<String, Object> paramMap6 = new HashMap<>();
        String users = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllUser", paramMap6);
        List<VUserData> userDataList = JSON.parseArray(users, VUserData.class);
        //12345 -李XX
        Map<String, String> userKV = userDataList.stream().collect(Collectors.toMap(VUserData::getAccountId, VUserData::getName));
        //12345-lixx
        Map<String, String> userKV2 = userDataList.stream().collect(Collectors.toMap(VUserData::getAccountId, VUserData::getAccountName));
        //查询出所有的 epic
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("systemId", projectId);
        String epics = HttpUtil.post(URLConfig.CONSUMER_URL + "queryEpicBySystemId", paramMap);
        List<TEpic> epicList = JSON.parseArray(epics, TEpic.class);


        //查询出所有的 story
        HashMap<String, Object> paramMap2 = new HashMap<>();
        paramMap2.put("systemId", projectId);
        String storys = HttpUtil.post(URLConfig.CONSUMER_URL + "queryStoryBySystemId", paramMap2);
        List<TStory> storyList = JSON.parseArray(storys, TStory.class);
        Map<String, List<TStory>> storyListMap;
        if (Optional.ofNullable(storyList).isPresent()) {
            storyListMap = storyList.stream().collect(Collectors.groupingBy(TStory::getEpicId));
        } else {
            storyListMap = null;
        }


        //查询出所有的 task 任务
        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap3.put("systemId", projectId);
        String tasks = HttpUtil.post(URLConfig.CONSUMER_URL + "queryTaskBySystemId", paramMap3);
        List<TTask> tTasks = JSON.parseArray(tasks, TTask.class);
        Map<String, List<TTask>> taskListMap;
        if (Optional.ofNullable(tTasks).isPresent()) {
            taskListMap = tTasks.stream().collect(Collectors.groupingBy(TTask::getStoryId));
        } else {
            taskListMap = null;
        }


        List<DevopsTask> devopsTasks = new ArrayList<>();
        List<DevopsDemand> devopsDemands = new ArrayList<>();
        DevopsDemand epicDemand = null;
        for (TEpic e : epicList) {
            String epicId = e.getEpicId();
            epicDemand = new DevopsDemand();
            epicDemand.setId(epicId);
            epicDemand.setDemandSource(e.getSource() == null ? "4" : "3");
            epicDemand.setCode("D" + System.nanoTime());
            epicDemand.setDemandType(e.getType());
            epicDemand.setFullId(epicId);
            epicDemand.setIsLeaf("0");
            epicDemand.setManager(e.getMoniterUser());
            epicDemand.setManagerAccount(userKV2.get(e.getMoniterUser()));
            epicDemand.setManagerName(userKV.get(e.getMoniterUser()));
            epicDemand.setIntroducer(e.getMoniterUser());
            epicDemand.setIntroducerAccount(userKV2.get(e.getMoniterUser()));
            epicDemand.setIntroducerName(userKV.get(e.getMoniterUser()));
            epicDemand.setParentId("-1");
            epicDemand.setTaskGroupId(e.getTaskGroupId());
            epicDemand.setPlanDetailId(e.getPlaneDetailId());
            epicDemand.setPlanId(e.getPlaneId());
            epicDemand.setProjectId(e.getSystemId());
            epicDemand.setName(e.getTitle());
            epicDemand.setSort(e.getSort());
            epicDemand.setStatus(e.getStatus());
            epicDemand.setRemark(StringUtils.isBlank(e.getRemark()) ? e.getTitle() : e.getRemark());
            epicDemand.setCommitTime(LocalDateTime.now());
            epicDemand.setPredictBeginTime(e.getPredictBeginTime());
            epicDemand.setPredictEndTime(e.getPredictEndTime());
            epicDemand.setPracticalBeginTime(e.getPracticalBeginTime());
            epicDemand.setPracticalEndTime(e.getPracticalEndTime());
            epicDemand.setHoursOfWork(e.getHoursOfWork());
            epicDemand.setHoursOfWorkTruth(e.getHoursOfWorkTruth());
            epicDemand.setHoursOfWorkUnit(e.getHoursOfWorkUnit());
//                            .num((long)e.getSort())
            epicDemand.setLevel(e.getLevel());
            epicDemand.setIsValid("Y");
            epicDemand.setCreateTime(e.getCreateTime());
            epicDemand.setCreateUser(e.getCreateUser());
            epicDemand.setCreateUserName(e.getCreateUser() == null ? "devops" : (userKV.get(e.getCreateUser()) == null ? "devops" : userKV.get(e.getCreateUser())));
            epicDemand.setUpdateTime(e.getUpdateTime());
            epicDemand.setUpdateUser(e.getUpdateUser());
            epicDemand.setUpdateUserName(e.getUpdateUser() == null ? "devops" : (userKV.get(e.getUpdateUser()) == null ? "devops" : userKV.get(e.getUpdateUser())));
            epicDemand.setLesseeId("devops");
            devopsDemands.add(epicDemand);


            //处理 story
            if (Optional.ofNullable(storyListMap).isPresent() && storyListMap.get(e.getEpicId()) != null) {
                DevopsDemand storyDemand = null;
                List<TStory> tStories = storyListMap.get(epicId);
                for (TStory story : tStories) {
                    String storyId = story.getStoryId();
                    storyDemand = new DevopsDemand();
                    storyDemand.setId(story.getStoryId());
                    storyDemand.setDemandSource(story.getSource() == null ? "4" : "3");
                    storyDemand.setCode("D" + System.nanoTime());
                    storyDemand.setDemandType(story.getType());
                    storyDemand.setFullId(story.getEpicId() + "/" + story.getStoryId());
                    storyDemand.setIsLeaf("1");
                    storyDemand.setManager(story.getMoniterUser());
                    storyDemand.setManagerAccount(userKV2.get(story.getMoniterUser()));
                    storyDemand.setManagerName(userKV.get(story.getMoniterUser()));
                    storyDemand.setIntroducer(story.getMoniterUser());
                    storyDemand.setIntroducerAccount(userKV2.get(story.getMoniterUser()));
                    storyDemand.setIntroducerName(userKV.get(story.getMoniterUser()));
                    storyDemand.setParentId(story.getEpicId());
                    storyDemand.setTaskGroupId(story.getTaskGroupId());
                    storyDemand.setPlanDetailId(story.getPlaneDetailId());
                    storyDemand.setPlanId(story.getPlaneId());
                    storyDemand.setProjectId(story.getSystemId());
                    storyDemand.setName(story.getTitle());
                    storyDemand.setSort(story.getSort());
                    storyDemand.setStatus(story.getStatus());
                    storyDemand.setRemark(StringUtils.isBlank(story.getRemark()) ? story.getTitle() : story.getRemark());
                    storyDemand.setCommitTime(LocalDateTime.now());
                    storyDemand.setPredictBeginTime(story.getPredictBeginTime());
                    storyDemand.setPredictEndTime(story.getPredictEndTime());
                    storyDemand.setPracticalBeginTime(story.getPracticalBeginTime());
                    storyDemand.setPracticalEndTime(story.getPracticalEndTime());
                    storyDemand.setHoursOfWork(story.getHoursOfWork());
                    storyDemand.setHoursOfWorkTruth(story.getHoursOfWorkTruth());
                    storyDemand.setHoursOfWorkUnit(story.getHoursOfWorkUnit());
                    storyDemand.setLevel(story.getLevel());
                    storyDemand.setIsValid("Y");
                    storyDemand.setCreateTime(story.getCreateTime());
                    storyDemand.setCreateUser(story.getCreateUser());
                    storyDemand.setCreateUserName(story.getCreateUser() == null ? "devops" : (userKV.get(story.getCreateUser()) == null ? "devops" : userKV.get(story.getCreateUser())));
                    storyDemand.setUpdateTime(story.getUpdateTime());
                    storyDemand.setUpdateUser(story.getUpdateUser());
                    storyDemand.setUpdateUserName(story.getUpdateUser() == null ? "devops" : (userKV.get(story.getUpdateUser()) == null ? "devops" : userKV.get(story.getUpdateUser())));
                    storyDemand.setLesseeId("devops");
                    devopsDemands.add(storyDemand);
                    if (Optional.ofNullable(taskListMap).isPresent() && taskListMap.get(storyId) != null) {
                        List<TTask> tTasks1 = taskListMap.get(storyId);
                        DevopsTask devopsTask = null;
                        for (TTask t : tTasks1) {
                            devopsTask = new DevopsTask();
                            devopsTask.setId(t.getTaskId());
                            devopsTask.setCode("D" + System.nanoTime());
                            devopsTask.setFullId(t.getEpicId() + "/" + t.getStoryId() + "/" + t.getTaskId());
                            devopsTask.setIsLeaf("0");
                            devopsTask.setManager(t.getMoniterUser());
                            devopsTask.setManagerName(userKV.get(t.getMoniterUser()));
                            devopsTask.setVersion(1L);
                            devopsTask.setParentId(t.getStoryId());
                            devopsTask.setTaskGroupId(t.getTaskGroupId());
                            devopsTask.setPlanDetailId(t.getPlaneDetailId());
                            devopsTask.setPlanId(t.getPlaneId());
                            devopsTask.setProjectId(t.getSystemId());
                            devopsTask.setName(t.getTitle());
                            devopsTask.setSort(t.getSort());
                            devopsTask.setStatus(t.getStatus());
                            devopsTask.setRemark(StringUtils.isBlank(t.getRemark()) ? t.getTitle() : t.getRemark());
                            devopsTask.setPredictBeginTime(t.getPredictBeginTime());
                            devopsTask.setPredictEndTime(t.getPredictEndTime());
                            devopsTask.setPracticalBeginTime(t.getPracticalBeginTime());
                            devopsTask.setPracticalEndTime(t.getPracticalEndTime());
                            devopsTask.setHoursOfWork(t.getHoursOfWork());
                            devopsTask.setHoursOfWorkTruth(t.getHoursOfWorkTruth());
                            devopsTask.setHoursOfWorkUnit(t.getHoursOfWorkUnit());
                            devopsTask.setSource(StringUtils.isBlank(t.getSource()) ? "1" : "2");
                            devopsTask.setTaskType(t.getType());
                            devopsTask.setLevel(t.getLevel());
                            devopsTask.setIsValid("Y");
                            devopsTask.setCreateTime(t.getCreateTime());
                            devopsTask.setCreateUser(t.getCreateUser());
                            devopsTask.setUpdateTime(t.getUpdateTime());
                            devopsTask.setUpdateUser(t.getUpdateUser());
                            devopsTask.setCreateUserName(t.getCreateUser() == null ? "devops" : (userKV.get(t.getCreateUser()) == null ? "devops" : userKV.get(t.getCreateUser())));
                            devopsTask.setUpdateUserName(t.getUpdateUser() == null ? "devops" : (userKV.get(t.getUpdateUser()) == null ? "devops" : userKV.get(t.getUpdateUser())));
                            devopsTask.setLesseeId("devops");
                            devopsTasks.add(devopsTask);
                        }
                    }
                }
            }
        }
        if (CollectionUtils.isNotEmpty(devopsTasks)) {
            devopsTaskService.saveTaskList(devopsTasks);
        }
        if (CollectionUtils.isNotEmpty(devopsDemands)) {
            devopsDemandService.saveDemandList(devopsDemands);
        }
    }

    public void migrateProductTask(String productId) {

        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProductPlan", paramMap4);
        List<TProductPlane> planeList = JSON.parseArray(plans, TProductPlane.class);

        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap3.put("systemId", productId);
        String tasks = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProductTaskBySystemId", paramMap3);
        List<TProductTask> tTasks = JSON.parseArray(tasks, TProductTask.class);
        List<DevopsTask> list = new ArrayList<>();
        DevopsTask devopsTask = null;
        for (TProductTask tTask : tTasks) {
            if (StringUtils.isNotBlank(tTask.getRemark())) {
                devopsTask = new DevopsTask();
                devopsTask.setId(tTask.getProductTaskId());
                devopsTask.setRemark(tTask.getRemark());
                list.add(devopsTask);
            }
        }
        devopsTaskService.updateBatchById(list);
    }


    public void migrateProductDemand(String productId) {
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProductPlan", paramMap4);
        List<TProductPlane> planeList = JSON.parseArray(plans, TProductPlane.class);

        HashMap<String, Object> paramMap6 = new HashMap<>();
        String users = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllUser", paramMap6);
        List<VUserData> userDataList = JSON.parseArray(users, VUserData.class);

        Map<String, String> userKV = userDataList.stream().collect(Collectors.toMap(VUserData::getAccountId, VUserData::getName));
        Map<String, String> userKV2 = userDataList.stream().collect(Collectors.toMap(VUserData::getAccountId, VUserData::getAccountName));

        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("systemId", productId);
        String epics = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProductEpicBySystemId", paramMap);
        List<TProductEpic> epicList = JSON.parseArray(epics, TProductEpic.class);


        HashMap<String, Object> paramMap2 = new HashMap<>();
        paramMap2.put("systemId", productId);
        String storys = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProductStoryBySystemId", paramMap2);
        List<TProductStory> storyList = JSON.parseArray(storys, TProductStory.class);
        Map<String, List<TProductStory>> storyListMap;
        if (Optional.ofNullable(storyList).isPresent()) {
            storyListMap = storyList.stream().collect(Collectors.groupingBy(TProductStory::getProductEpicId));
        } else {
            storyListMap = null;
        }

        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap3.put("systemId", productId);
        String tasks = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProductTaskBySystemId", paramMap3);
        List<TProductTask> tTasks = JSON.parseArray(tasks, TProductTask.class);
        Map<String, List<TProductTask>> taskListMap;
        if (Optional.ofNullable(tTasks).isPresent()) {
            taskListMap = tTasks.stream().collect(Collectors.groupingBy(TProductTask::getProductStoryId));
        } else {
            taskListMap = null;
        }

        List<DevopsDemand> devopsDemands = new ArrayList<>();
        List<DevopsTask> devopsTasks = new ArrayList<>();


        DevopsDemand epicDemand = null;
        for (TProductEpic e : epicList) {
            String epicId = e.getProductEpicId();
            epicDemand = new DevopsDemand();
            epicDemand.setId(epicId);
            if (!Optional.ofNullable(e.getSource()).isPresent() || e.getSource().equals("1") || StringUtils.isBlank(e.getSource())) {
                epicDemand.setDemandSource("4");
            }
            if (Optional.ofNullable(e.getSource()).isPresent() && e.getSource().equals("2")) {
                epicDemand.setDemandSource("3");
            }

            epicDemand.setCode("D" + System.nanoTime());
            epicDemand.setDemandType(e.getType());
            epicDemand.setFullId(epicId);
            epicDemand.setIsLeaf("0");
            epicDemand.setManager(e.getMoniterUser());
            epicDemand.setManagerAccount(userKV2.get(e.getMoniterUser()));
            epicDemand.setManagerName(userKV.get(e.getMoniterUser()));
            epicDemand.setIntroducer(e.getMoniterUser());
            epicDemand.setIntroducerAccount(userKV2.get(e.getMoniterUser()));
            epicDemand.setIntroducerName(userKV.get(e.getMoniterUser()));
            epicDemand.setParentId("-1");
            epicDemand.setTaskGroupId(e.getProductTaskGroupId());
            epicDemand.setPlanDetailId(e.getProductPlaneDetailId());
            epicDemand.setPlanId(e.getProductPlaneId());
            epicDemand.setProjectId(e.getSystemId());
            epicDemand.setName(e.getTitle());
            epicDemand.setSort(e.getSort());
            epicDemand.setStatus(e.getStatus());

            epicDemand.setRemark(StringUtils.isBlank(e.getRemark()) ? e.getTitle() : e.getRemark());

            epicDemand.setCommitTime(LocalDateTime.now());
            epicDemand.setPredictBeginTime(e.getPredictBeginTime());
            epicDemand.setPredictEndTime(e.getPredictEndTime());
            epicDemand.setPracticalBeginTime(e.getPracticalBeginTime());
            epicDemand.setPracticalEndTime(e.getPracticalEndTime());
            epicDemand.setHoursOfWork(e.getHoursOfWork());
            epicDemand.setHoursOfWorkTruth(e.getHoursOfWorkTruth());
            epicDemand.setHoursOfWorkUnit(e.getHoursOfWorkUnit());
            epicDemand.setLevel(e.getLevel());
            epicDemand.setIsValid("Y");
            epicDemand.setCreateTime(e.getCreateTime());
            epicDemand.setCreateUser(e.getCreateUser());
            epicDemand.setCreateUserName(e.getCreateUser() == null ? "devops" : (userKV.get(e.getCreateUser()) == null ? "devops" : userKV.get(e.getCreateUser())));
            epicDemand.setUpdateTime(e.getUpdateTime());
            epicDemand.setUpdateUser(e.getUpdateUser());
            epicDemand.setUpdateUserName(e.getUpdateUser() == null ? "devops" : (userKV.get(e.getUpdateUser()) == null ? "devops" : userKV.get(e.getUpdateUser())));
            epicDemand.setLesseeId("devops");
            devopsDemands.add(epicDemand);
            //处理 story
            if (Optional.ofNullable(storyListMap).isPresent() && storyListMap.get(e.getProductEpicId()) != null) {
                DevopsDemand storyDemand = null;
                List<TProductStory> tStories = storyListMap.get(epicId);
                for (TProductStory story : tStories) {
                    String storyId = story.getProductStoryId();
                    storyDemand = new DevopsDemand();
                    storyDemand.setId(storyId);
                    if (!Optional.ofNullable(e.getSource()).isPresent() || e.getSource().equals("1") || StringUtils.isBlank(e.getSource())) {
                        storyDemand.setDemandSource("4");
                    }
                    if (Optional.ofNullable(e.getSource()).isPresent() && e.getSource().equals("2")) {
                        storyDemand.setDemandSource("3");
                    }
                    storyDemand.setCode("D" + System.nanoTime());
                    storyDemand.setDemandType(story.getType());
                    storyDemand.setFullId(epicId + "/" + storyId);
                    storyDemand.setIsLeaf("1");
                    storyDemand.setManager(story.getMoniterUser());
                    storyDemand.setManagerAccount(userKV2.get(story.getMoniterUser()));
                    storyDemand.setManagerName(userKV.get(story.getMoniterUser()));
                    storyDemand.setIntroducer(story.getMoniterUser());
                    storyDemand.setIntroducerAccount(userKV2.get(story.getMoniterUser()));
                    storyDemand.setIntroducerName(userKV.get(story.getMoniterUser()));
                    storyDemand.setParentId(story.getProductEpicId());
                    storyDemand.setTaskGroupId(story.getProductTaskGroupId());
                    storyDemand.setPlanDetailId(story.getProductPlaneDetailId());
                    storyDemand.setPlanId(story.getProductPlaneId());
                    storyDemand.setProjectId(story.getSystemId());
                    storyDemand.setName(story.getTitle());
                    storyDemand.setSort(story.getSort());
                    storyDemand.setStatus(story.getStatus());
                    storyDemand.setRemark(StringUtils.isBlank(story.getRemark()) ? story.getTitle() : story.getRemark());
                    storyDemand.setCommitTime(LocalDateTime.now());
                    storyDemand.setPredictBeginTime(story.getPredictBeginTime());
                    storyDemand.setPredictEndTime(story.getPredictEndTime());
                    storyDemand.setPracticalBeginTime(story.getPracticalBeginTime());
                    storyDemand.setPracticalEndTime(story.getPracticalEndTime());
                    storyDemand.setHoursOfWork(story.getHoursOfWork());
                    storyDemand.setHoursOfWorkTruth(story.getHoursOfWorkTruth());
                    storyDemand.setHoursOfWorkUnit(story.getHoursOfWorkUnit());
                    storyDemand.setLevel(story.getLevel());
                    storyDemand.setIsValid("Y");
                    storyDemand.setCreateTime(story.getCreateTime());
                    storyDemand.setCreateUser(story.getCreateUser());
                    storyDemand.setCreateUserName(story.getCreateUser() == null ? "devops" : (userKV.get(story.getCreateUser()) == null ? "devops" : userKV.get(story.getCreateUser())));
                    storyDemand.setUpdateTime(story.getUpdateTime());
                    storyDemand.setUpdateUser(story.getUpdateUser());
                    storyDemand.setUpdateUserName(story.getUpdateUser() == null ? "devops" : (userKV.get(story.getUpdateUser()) == null ? "devops" : userKV.get(story.getUpdateUser())));
                    storyDemand.setLesseeId("devops");
                    devopsDemands.add(storyDemand);
                    if (Optional.ofNullable(taskListMap).isPresent() && taskListMap.get(storyId) != null) {
                        List<TProductTask> tTasks1 = taskListMap.get(storyId);
                        DevopsTask devopsTask = null;
                        for (TProductTask t : tTasks1) {
                            devopsTask = new DevopsTask();
                            String taskId = t.getProductTaskId();
                            devopsTask.setId(taskId);
                            devopsTask.setCode("D" + System.nanoTime());
                            devopsTask.setFullId(epicId + "/" + storyId + "/" + taskId);
                            devopsTask.setIsLeaf("0");
                            devopsTask.setParentId(storyId);
                            devopsTask.setManager(t.getMoniterUser());
                            devopsTask.setManagerName(userKV.get(t.getMoniterUser()));
                            devopsTask.setVersion(1L);
                            devopsTask.setSource(StringUtils.isBlank(t.getSource()) ? "1" : "2");
                            devopsTask.setTaskType(t.getType());
                            devopsTask.setParentId(storyId);
                            devopsTask.setTaskGroupId(t.getProductTaskGroupId());
                            devopsTask.setPlanDetailId(t.getProductPlaneDetailId());
                            devopsTask.setPlanId(t.getProductPlaneId());
                            devopsTask.setProjectId(t.getSystemId());
                            devopsTask.setName(t.getTitle());
                            devopsTask.setRemark(t.getRemark());
                            devopsTask.setSort(t.getSort());
                            devopsTask.setStatus(t.getStatus());
                            devopsTask.setPredictBeginTime(t.getPredictBeginTime());
                            devopsTask.setPredictEndTime(t.getPredictEndTime());
                            devopsTask.setPracticalBeginTime(t.getPracticalBeginTime());
                            devopsTask.setPracticalEndTime(t.getPracticalEndTime());
                            devopsTask.setHoursOfWork(t.getHoursOfWork());
                            devopsTask.setHoursOfWorkTruth(t.getHoursOfWorkTruth());
                            devopsTask.setHoursOfWorkUnit(t.getHoursOfWorkUnit());
                            devopsTask.setLevel(t.getLevel());
                            devopsTask.setIsValid("Y");
                            devopsTask.setCreateTime(t.getCreateTime());
                            devopsTask.setCreateUser(t.getCreateUser());
                            devopsTask.setUpdateTime(t.getUpdateTime());
                            devopsTask.setUpdateUser(t.getUpdateUser());
                            devopsTask.setCreateUserName(t.getCreateUser() == null ? "devops" : (userKV.get(t.getCreateUser()) == null ? "devops" : userKV.get(t.getCreateUser())));
                            devopsTask.setUpdateUserName(t.getUpdateUser() == null ? "devops" : (userKV.get(t.getUpdateUser()) == null ? "devops" : userKV.get(t.getUpdateUser())));
                            devopsTask.setLesseeId("devops");
                            devopsTasks.add(devopsTask);
                        }
                    }
                }
            }
        }
        if (Optional.ofNullable(devopsTasks).isPresent()) {
            devopsTaskService.saveTaskList(devopsTasks);
        }
        if (Optional.ofNullable(devopsDemands).isPresent()) {
//            devopsDemandService.saveDemandList(devopsDemands);
        }
    }

    public void migrateProductBucket(String systemId) {
        //查询所有的产品桶
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("planId", systemId);
        String result = HttpUtil.post(URLConfig.CONSUMER_URL + "queryByPlanId", paramMap);
        //查询所有的产品计划
        HashMap<String, Object> paramMap2 = new HashMap<>();
        paramMap2.put("systemId", systemId);
        String result2 = HttpUtil.post(URLConfig.CONSUMER_URL + "queryProductPlanBySystemId", paramMap2);
        List<TProductPlane> plane = JSON.parseArray(result2, TProductPlane.class);
        Map<Integer, String> planMap = plane.stream().collect(Collectors.toMap(TProductPlane::getProductPlaneYear, TProductPlane::getProductPlaneId));

        List<TBucket> tBuckets = JSON.parseArray(result, TBucket.class);
        List<DevopsBucket> devopsBuckets = new ArrayList<>();
        tBuckets.forEach(e -> {
            DevopsBucket build = DevopsBucket.builder()
                    //转为主计划 ID
                    .bucketId(planMap.get(e.getCreateTime().getYear())).bucketName(e.getBucketName()).isValid("Y").lesseeId("devops").systemType(e.getSystemType()).createTime(e.getCreateTime()).createUser(e.getCreateUser()).createUserName(e.getCreateUserName()).updateTime(e.getUpdateTime()).updateUser(e.getUpdateUser()).updateUserName(e.getUpdateUserName()).planId(planMap.get(e.getCreateTime().getYear())).build();
            devopsBuckets.add(build);
        });
        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap3.put("projectId", systemId);
        String result3 = HttpUtil.post(URLConfig.CONSUMER_URL + "queryBucketFileByProjectId", paramMap3);
        List<TBucketFile> tBucketFiles = JSON.parseArray(result3, TBucketFile.class);

        List<DevopsBucketFile> devopsBucketFiles = new ArrayList<>();
        tBucketFiles.forEach(e -> {
            DevopsBucketFile build = DevopsBucketFile.builder().bucketFileId(e.getBucketFileId()).bucketFilePid(e.getBucketFilePid()).bucketFileCode(e.getBucketFileCode()).bucketFileName(e.getBucketFileName()).bucketFileRemark(e.getBucketFileRemark()).bucketFileType(e.getBucketFileType()).bucketFileUrl(e.getBucketFileUrl())
                    //转为主计划 ID
                    .bucketId(planMap.get(e.getCreateTime().getYear())).globalId(e.getSeq()).isValid("Y").lesseeId("devops").createTime(e.getCreateTime()).createUser(e.getCreateUser()).createUserName(e.getCreateUserName()).updateTime(e.getUpdateTime()).updateUser(e.getUpdateUser()).updateUserName(e.getUpdateUserName()).source("5").build();
            devopsBucketFiles.add(build);
        });
        if (!devopsBuckets.isEmpty()) {
            devopsBucketService.saveBatch(devopsBuckets);
        }
        if (!devopsBucketFiles.isEmpty()) {
            devopsBucketFileService.saveBucketFile(devopsBucketFiles);
        }
    }
//============


    public void migrateDemandProject(String projectId) {
        //查询所有计划
        HashMap<String, Object> paramMap4 = new HashMap<>();
        String plans = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllProjectPlan", paramMap4);
        List<TPlane> planeList = JSON.parseArray(plans, TPlane.class);
        Map<String, String> planMap = planeList.stream().collect(Collectors.toMap(TPlane::getSystemId, TPlane::getPlaneId));

        HashMap<String, Object> paramMap6 = new HashMap<>();


        //查询所有用户
        String users = HttpUtil.post(URLConfig.CONSUMER_URL + "queryAllUser", paramMap6);
        List<VUserData> userDataList = JSON.parseArray(users, VUserData.class);

        //12345 -李XX
        Map<String, String> userKV = userDataList.stream().collect(Collectors.toMap(VUserData::getAccountId, VUserData::getName));
        //12345-lixx
        Map<String, String> userKV2 = userDataList.stream().collect(Collectors.toMap(VUserData::getAccountId, VUserData::getAccountName));
        //查询出所有的 epic
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("systemId", projectId);
        String epics = HttpUtil.post(URLConfig.CONSUMER_URL + "queryEpicBySystemId", paramMap);
        List<TEpic> epicList = JSON.parseArray(epics, TEpic.class);

        //查询出所有的 story
        HashMap<String, Object> paramMap2 = new HashMap<>();
        paramMap2.put("systemId", projectId);
        String storys = HttpUtil.post(URLConfig.CONSUMER_URL + "queryStoryBySystemId", paramMap2);
        List<TStory> storyList = JSON.parseArray(storys, TStory.class);

        //查询出所有的 task 任务
        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap3.put("systemId", projectId);
        String tasks = HttpUtil.post(URLConfig.CONSUMER_URL + "queryTaskBySystemId", paramMap3);
        List<TTask> tTasks = JSON.parseArray(tasks, TTask.class);

        HashMap<String, Object> paramMap5 = new HashMap<>();
        paramMap5.put("projectId", projectId);
        String buckets = HttpUtil.post(URLConfig.CONSUMER_URL + "queryBucketByProjectId", paramMap5);
        List<TBucket> tBucketList = JSON.parseArray(buckets, TBucket.class);


        List<DevopsDemand> devopsDemands = new ArrayList<>();
        List<DevopsTask> devopsTasks = new ArrayList<>();
        List<DevopsBucket> devopsBuckets = new ArrayList<>();


        //保存 bucket
        if (Optional.ofNullable(tBucketList).isPresent()) {
            for (TBucket tBucket : tBucketList) {
                DevopsBucket devopsBucket = DevopsBucket.builder().bucketId(planMap.get(tBucket.getSystemId())).systemType("1").planId(planMap.get(tBucket.getSystemId())).bucketName(tBucket.getBucketName()).createUserName(userKV.get(tBucket.getCreateUser())).createUser(tBucket.getCreateUser()).createTime(tBucket.getCreateTime()).updateTime(tBucket.getUpdateTime()).isValid("Y").lesseeId("devops").updateUser(tBucket.getUpdateUser()).updateUserName(userKV.get(tBucket.getUpdateUser())).build();
                devopsBuckets.add(devopsBucket);
            }
            devopsBucketService.saveBatch(devopsBuckets);
        }

        HashMap<String, Object> paramMap8 = new HashMap<>();
        paramMap8.put("projectId", projectId);
        String bucketFiles = HttpUtil.post(URLConfig.CONSUMER_URL + "queryBucketFileByProjectId", paramMap8);
        List<TBucketFile> bucketFileList = JSON.parseArray(bucketFiles, TBucketFile.class);
        List<DevopsBucketFile> devopsBucketFiles = new ArrayList<>();
        if (Optional.ofNullable(bucketFileList).isPresent()) {
            bucketFileList.forEach(e -> {
                DevopsBucketFile build = DevopsBucketFile.builder().bucketFileId(e.getBucketFileId()).bucketFilePid(e.getBucketFilePid()).bucketFileCode(e.getBucketFileCode()).bucketFileName(e.getBucketFileName()).bucketFileRemark(e.getBucketFileRemark()).bucketFileType(e.getBucketFileType()).bucketFileUrl(e.getBucketFileUrl())
                        //转为主计划 ID
                        .bucketId(planMap.get(e.getBucketId())).globalId(e.getSeq()).isValid("Y").lesseeId("devops").createTime(e.getCreateTime()).createUser(e.getCreateUser()).createUserName(e.getCreateUserName()).updateTime(e.getUpdateTime()).updateUser(e.getUpdateUser()).updateUserName(e.getUpdateUserName()).source("5").build();
                devopsBucketFiles.add(build);
            });
            devopsBucketFileService.saveBucketFile(devopsBucketFiles);
        }
        if (Optional.ofNullable(epicList).isPresent()) {
            epicList.forEach(e -> {
                DevopsDemand build = DevopsDemand.builder().id(e.getEpicId()).demandSource(e.getSource() == null ? "4" : "3").code(StringUtils.isBlank(e.getCode()) ? "D" + System.nanoTime() : e.getCode()).demandType(e.getType()).fullId(e.getEpicId()).isLeaf("0").manager(e.getMoniterUser()).managerAccount(userKV2.get(e.getMoniterUser())).managerName(userKV.get(e.getMoniterUser())).introducer(e.getMoniterUser()).introducerAccount(userKV2.get(e.getMoniterUser())).introducerName(userKV.get(e.getMoniterUser())).parentId("-1").taskGroupId(e.getTaskGroupId()).planDetailId(e.getPlaneDetailId()).planId(e.getPlaneId()).projectId(e.getSystemId()).name(e.getTitle()).sort(e.getSort()).status(e.getStatus()).remark(StringUtils.isBlank(e.getRemark()) ? e.getTitle() : e.getRemark()).commitTime(LocalDateTime.now()).predictBeginTime(e.getPredictBeginTime()).predictEndTime(e.getPredictEndTime()).practicalBeginTime(e.getPracticalBeginTime()).practicalEndTime(e.getPracticalEndTime()).hoursOfWork(e.getHoursOfWork()).hoursOfWorkTruth(e.getHoursOfWorkTruth()).hoursOfWorkUnit(e.getHoursOfWorkUnit())
//                            .num((long)e.getSort())
                        .level(e.getLevel()).isValid("Y").createTime(e.getCreateTime()).createUser(e.getCreateUser()).createUserName(userKV.get(e.getCreateUser())).updateTime(e.getUpdateTime()).updateUser(e.getUpdateUser()).updateUserName(userKV.get(e.getUpdateUser())).lesseeId("devops").build();
                devopsDemands.add(build);
            });
            devopsDemandService.saveDemandList(devopsDemands);
            devopsDemands.clear();
        }

        if (Optional.ofNullable(storyList).isPresent()) {
            storyList.forEach(e -> {
                DevopsDemand build = DevopsDemand.builder().id(e.getStoryId()).demandSource(e.getSource() == null ? "4" : "3").code(StringUtils.isBlank(e.getCode()) ? "D" + System.nanoTime() : e.getCode()).demandType(e.getType()).fullId(e.getEpicId() + "/" + e.getStoryId()).isLeaf("1").manager(e.getMoniterUser()).managerAccount(userKV2.get(e.getMoniterUser())).managerName(userKV.get(e.getMoniterUser())).introducer(e.getMoniterUser()).introducerAccount(userKV2.get(e.getMoniterUser())).introducerName(userKV.get(e.getMoniterUser())).parentId(e.getEpicId()).taskGroupId(e.getTaskGroupId()).planDetailId(e.getPlaneDetailId()).planId(e.getPlaneId()).projectId(e.getSystemId()).name(e.getTitle()).sort(e.getSort()).status(e.getStatus()).remark(StringUtils.isBlank(e.getRemark()) ? e.getTitle() : e.getRemark()).commitTime(LocalDateTime.now()).predictBeginTime(e.getPredictBeginTime()).predictEndTime(e.getPredictEndTime()).practicalBeginTime(e.getPracticalBeginTime()).practicalEndTime(e.getPracticalEndTime()).hoursOfWork(e.getHoursOfWork()).hoursOfWorkTruth(e.getHoursOfWorkTruth()).hoursOfWorkUnit(e.getHoursOfWorkUnit())
//                            .num((long)e.getSort())
                        .level(e.getLevel()).isValid("Y").createTime(e.getCreateTime()).createUser(e.getCreateUser()).createUserName(userKV.get(e.getCreateUser()) == null ? "devops" : userKV.get(e.getCreateUser())).updateUserName(userKV.get(e.getUpdateUser()) == null ? "devops" : userKV.get(e.getUpdateUser())).updateTime(e.getUpdateTime()).updateUser(e.getUpdateUser())

                        .lesseeId("devops").build();
                devopsDemands.add(build);
            });
            devopsDemandService.saveDemandList(devopsDemands);
            devopsDemands.clear();
        }

        if (Optional.ofNullable(tTasks).isPresent()) {
            tTasks.forEach(t -> {
//                DevopsTask build = DevopsTask.builder()
//                        .id(e.getTaskId())
//                        .code(StringUtils.isBlank(e.getCode()) ? "D" + System.nanoTime() : e.getCode())
//                        .fullId(e.getEpicId() + "/" + e.getStoryId() + "/" + e.getTaskId())
//                        .isLeaf("0")
//                        .manager(e.getMoniterUser())
//                        .managerName(userKV.get(e.getMoniterUser()))
//                        .version(1L)
//                        .parentId(e.getStoryId())
//                        .taskGroupId(e.getTaskGroupId())
//                        .planDetailId(e.getPlaneDetailId())
//                        .planId(e.getPlaneId())
//                        .projectId(e.getSystemId())
//                        .name(e.getTitle())
//                        .sort(e.getSort())
//                        .status(e.getStatus())
//                        .remark(StringUtils.isBlank(e.getRemark()) ? e.getTitle() : e.getRemark())
//                        .predictBeginTime(e.getPredictBeginTime())
//                        .predictEndTime(e.getPredictEndTime())
//                        .practicalBeginTime(e.getPracticalBeginTime())
//                        .practicalEndTime(e.getPracticalEndTime())
//                        .hoursOfWork(e.getHoursOfWork())
//                        .hoursOfWorkTruth(e.getHoursOfWorkTruth())
//                        .hoursOfWorkUnit(e.getHoursOfWorkUnit())
////                            .num((long)e.getSort())
//                        .level(e.getLevel())
//                        .isValid("Y")
//                        .createTime(e.getCreateTime())
//                        .createUser(e.getCreateUser())
//                        .updateTime(e.getUpdateTime())
//                        .updateUser(e.getUpdateUser())
//                        .createUserName(userKV.get(e.getCreateUser()) == null ? "devops" : userKV.get(e.getCreateUser()))
//                        .updateUserName(userKV.get(e.getUpdateUser()) == null ? "devops" : userKV.get(e.getUpdateUser()))
//                        .lesseeId("devops")
//                        .build();
//                devopsTasks.add(build);
                DevopsTask devopsTask = new DevopsTask();
//                                DevopsTask taskBuild = DevopsTask.builder()
                devopsTask.setId(t.getTaskId());
                devopsTask.setCode(StringUtils.isBlank(t.getCode()) ? "D" + System.nanoTime() : t.getCode());
                devopsTask.setFullId(t.getEpicId() + "/" + t.getStoryId() + "/" + t.getTaskId());
                devopsTask.setIsLeaf("0");
                devopsTask.setManager(t.getMoniterUser());
                devopsTask.setManagerName(userKV.get(t.getMoniterUser()));
                devopsTask.setVersion(1L);
                devopsTask.setParentId(t.getStoryId());
                devopsTask.setTaskGroupId(t.getTaskGroupId());
                devopsTask.setPlanDetailId(t.getPlaneDetailId());
                devopsTask.setPlanId(t.getPlaneId());
                devopsTask.setProjectId(t.getSystemId());
                devopsTask.setName(t.getTitle());
                devopsTask.setSort(t.getSort());
                devopsTask.setStatus(t.getStatus());
                devopsTask.setRemark(StringUtils.isBlank(t.getRemark()) ? t.getTitle() : t.getRemark());
                devopsTask.setPredictBeginTime(t.getPredictBeginTime());
                devopsTask.setPredictEndTime(t.getPredictEndTime());
                devopsTask.setPracticalBeginTime(t.getPracticalBeginTime());
                devopsTask.setPracticalEndTime(t.getPracticalEndTime());
                devopsTask.setHoursOfWork(t.getHoursOfWork());
                devopsTask.setHoursOfWorkTruth(t.getHoursOfWorkTruth());
                devopsTask.setHoursOfWorkUnit(t.getHoursOfWorkUnit());
//                            .num((long)e.getSort())
                devopsTask.setLevel(t.getLevel());
                devopsTask.setIsValid("Y");
                devopsTask.setCreateTime(t.getCreateTime());
                devopsTask.setCreateUser(t.getCreateUser());
                devopsTask.setUpdateTime(t.getUpdateTime());
                devopsTask.setUpdateUser(t.getUpdateUser());
                devopsTask.setCreateUserName(t.getCreateUser() == null ? "devops" : userKV.get(t.getCreateUser()));
                devopsTask.setUpdateUserName(t.getUpdateUser() == null ? "devops" : userKV.get(t.getUpdateUser()));
                devopsTask.setLesseeId("devops");
                devopsTasks.add(devopsTask);
            });
            devopsTaskService.saveTaskList(devopsTasks);
        }
    }


    public void createProductBucket(TProductPlane plane) throws Exception {
        String bucketId = plane.getProductPlaneId();
        System.out.println(bucketId);
        DevopsBucket bucket = new DevopsBucket();
        bucket.setBucketId(bucketId);
        bucket.setBucketName(plane.getProductPlaneName());
        bucket.setSystemType("2");
        bucket.setPlanId(bucketId);
        //先写入本地库
//        devopsBucketService.save(bucket);
        //在写入minion
//        minioUtil.createBucket("devops-" + plane.getProductPlaneId());
        //TODO 初始化文件夹
        initDir(bucketId);

    }

    public void createProjectBucket(TPlane plane) throws Exception {
        String bucketId = plane.getPlaneId();
        System.out.println(bucketId);
        DevopsBucket bucket = new DevopsBucket();
        bucket.setBucketId(bucketId);
        bucket.setBucketName(plane.getPlaneName());
        bucket.setSystemType("1");
        bucket.setPlanId(bucketId);
        //先写入本地库
//        devopsBucketService.save(bucket);
        //在写入minion
//        minioUtil.createBucket("devops-" + plane.getPlaneId());
        //TODO 初始化文件夹
        initDir(bucketId);

    }

    public void initDir(String bucketId) {
        DirectoryVo build = DirectoryVo.builder().bucketFileId("milestone-" + bucketId).dirName("里程碑").dirId("milestone-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build1 = DirectoryVo.builder().bucketFileId("task-" + bucketId).dirName("任务").dirId("task-" + bucketId).dirName("任务").bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build2 = DirectoryVo.builder().bucketFileId("test-" + bucketId).dirName("测试").dirId("test-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build3 = DirectoryVo.builder().bucketFileId("demand-" + bucketId).dirName("需求池").dirId("demand-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        DirectoryVo build4 = DirectoryVo.builder().bucketFileId("pack-" + bucketId).dirName("制品").dirId("pack-" + bucketId).bucketId(bucketId).dirRemark("默认文件夹").build();
        devopsBucketService.createDir(build);
        devopsBucketService.createDir(build2);
        devopsBucketService.createDir(build1);
        devopsBucketService.createDir(build3);
        devopsBucketService.createDir(build4);
    }

    public void moveProjectBucketFile(TPlane take) {

        String projectId = take.getSystemId();
        HashMap<String, Object> paramMap8 = new HashMap<>();
        paramMap8.put("projectId", projectId);
        String bucketFiles = HttpUtil.post(URLConfig.CONSUMER_URL + "queryBucketFileByProjectId", paramMap8);
        List<TBucketFile> bucketFileList = JSON.parseArray(bucketFiles, TBucketFile.class);
        if (bucketFileList.isEmpty()) {
            return;
        }
        if (bucketFileList.size() == 1) {
            TBucketFile bucketFile = bucketFileList.get(0);
            if (bucketFile.getBucketFileUrl().contains("里程碑")) {
                return;
            }
        }


        //查询出所有的 epic
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("systemId", projectId);
        String epics = HttpUtil.post(URLConfig.CONSUMER_URL + "queryEpicBySystemId", paramMap);
        List<TEpic> epicList = JSON.parseArray(epics, TEpic.class);


        //查询出所有的 story
        HashMap<String, Object> paramMap2 = new HashMap<>();
        paramMap2.put("systemId", projectId);
        String storys = HttpUtil.post(URLConfig.CONSUMER_URL + "queryStoryBySystemId", paramMap2);
        List<TStory> storyList = JSON.parseArray(storys, TStory.class);


        //查询出所有的 task 任务
        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap3.put("systemId", projectId);
        String tasks = HttpUtil.post(URLConfig.CONSUMER_URL + "queryTaskBySystemId", paramMap3);
        List<TTask> tTasks = JSON.parseArray(tasks, TTask.class);

        //处理里程碑内的数据
        List<TBucketFile> bucketFileList1 = bucketFileList.stream().filter(e -> e.getBucketFileUrl().contains("里程碑") && !e.getBucketFileCode().equals("lichengbei")).collect(Collectors.toList());
        Map<String, String> collect = bucketFileList.stream().collect(Collectors.toMap(TBucketFile::getBucketFileUrl, TBucketFile::getBucketFileId));
        DevopsBucketFile devopsBucketFile = null;
        List<DevopsBucketFile> result = new ArrayList<>();
        for (TBucketFile bucketFile : bucketFileList1) {
            String pid = "";
            String fileName = "";
            if (bucketFile.getBucketFileType().equals("f")) {
                int i = bucketFile.getBucketFileUrl().lastIndexOf("/");
                String substring = bucketFile.getBucketFileUrl().substring(0, i + 1);
                if (substring.equals("/" + take.getSystemId() + "/" + "里程碑" + "/")) {
                    pid = "milestone-" + take.getPlaneId();
                } else {
                    pid = collect.get(substring);
                }
                int i1 = bucketFile.getBucketFileUrl().lastIndexOf("/");
                //文件名称
                fileName = bucketFile.getBucketFileUrl().substring(i1 + 1);
            }
            if (bucketFile.getBucketFileType().equals("d")) {
                String url = bucketFile.getBucketFileUrl().substring(0, bucketFile.getBucketFileUrl().length() - 1);
                int i = url.lastIndexOf("/");
                fileName = url.substring(i + 1);
                String prePath = url.substring(0, i + 1);
                if (prePath.equals("/" + take.getSystemId() + "/" + "里程碑" + "/")) {
                    pid = "milestone-" + take.getPlaneId();
                } else {
                    pid = collect.get(prePath);
                }

            }

            devopsBucketFile = new DevopsBucketFile();
            devopsBucketFile.setBucketFileId(bucketFile.getBucketFileId());
            devopsBucketFile.setBucketId(take.getPlaneId());
            devopsBucketFile.setBucketFilePid(pid);
            devopsBucketFile.setBucketFileType(bucketFile.getBucketFileType());
            devopsBucketFile.setBucketFileName(fileName);
            devopsBucketFile.setBucketFileCode(PinyinUtil.getPinyin(fileName));
            devopsBucketFile.setBucketFileUrl(bucketFile.getBucketFileUrl().replace(take.getSystemId(), take.getPlaneId()));
            devopsBucketFile.setBucketFileRemark(bucketFile.getBucketFileRemark());
            devopsBucketFile.setGlobalId("");
            devopsBucketFile.setSource("3");
            devopsBucketFile.setFileType(bucketFile.getFileType());
            devopsBucketFile.setCreateUser("devops");
            devopsBucketFile.setCreateUserName("devops");
            devopsBucketFile.setUpdateTime(LocalDateTime.now());
            devopsBucketFile.setCreateTime(LocalDateTime.now());
            devopsBucketFile.setUpdateUser("devops");
            devopsBucketFile.setUpdateUserName("devops");
            devopsBucketFile.setIsValid("Y");
            devopsBucketFile.setLesseeId("devops");

            String replace = bucketFile.getBucketFileUrl().replace(take.getSystemId() + "/", "");
//            minioUtil.copyObject(take.getSystemId(), "/里程碑/settings.xml", "devops-" + take.getPlaneId(), replace.substring(1));
            minioUtil.copyObject(take.getSystemId().toLowerCase(), replace, "devops-" + take.getPlaneId(), replace);
            result.add(devopsBucketFile);
        }

        for (TBucketFile bucketFile : bucketFileList) {
            if (bucketFile.getBucketFileUrl().contains("里程碑")) {
                continue;
            }
            int i = bucketFile.getBucketFileUrl().lastIndexOf("/");
            String substring2 = bucketFile.getBucketFileUrl().substring(i);
            String substring = bucketFile.getBucketFileUrl().substring(0, i + 1);
            devopsBucketFile = new DevopsBucketFile();
            devopsBucketFile.setBucketFileId(bucketFile.getBucketFileId());
            devopsBucketFile.setBucketId(take.getPlaneId());
            devopsBucketFile.setBucketFilePid("task-" + take.getPlaneId());
            devopsBucketFile.setBucketFileType(bucketFile.getBucketFileType());
            devopsBucketFile.setBucketFileName(bucketFile.getBucketFileName());
            devopsBucketFile.setBucketFileCode(bucketFile.getBucketFileCode());
            devopsBucketFile.setBucketFileUrl(bucketFile.getBucketFileUrl().replace(take.getSystemId(), take.getPlaneId()));
            devopsBucketFile.setBucketFileRemark(bucketFile.getBucketFileRemark());
            devopsBucketFile.setFileType(FileNameUtil.getSuffix(substring2));
            devopsBucketFile.setCreateUser("devops");
            devopsBucketFile.setCreateUserName("devops");
            devopsBucketFile.setUpdateTime(LocalDateTime.now());
            devopsBucketFile.setCreateTime(LocalDateTime.now());
            devopsBucketFile.setUpdateUser("devops");
            devopsBucketFile.setUpdateUserName("devops");
            devopsBucketFile.setIsValid("Y");
            devopsBucketFile.setLesseeId("devops");

            if (StringUtils.isNotBlank(bucketFile.getSeq())) {
                //epic附件
                for (TEpic tEpic : epicList) {
                    if (tEpic.getEpicId().equals(bucketFile.getSeq())) {
                        if (bucketFile.getBucketFileType().equals("f")) {
                            devopsBucketFile.setGlobalId(tEpic.getEpicId());
                            devopsBucketFile.setSource("2");
                            result.add(devopsBucketFile);
                        }
                    }
                }

                //story附件
                for (TStory tStory : storyList) {
                    if (tStory.getStoryId().equals(bucketFile.getSeq())) {
                        if (bucketFile.getBucketFileType().equals("f")) {
                            devopsBucketFile.setGlobalId(tStory.getStoryId());
                            devopsBucketFile.setSource("2");
                            result.add(devopsBucketFile);
                        }
                    }
                }

                //task附件
                for (TTask tTask : tTasks) {
                    if (tTask.getTaskId().equals(bucketFile.getSeq())) {
                        if (bucketFile.getBucketFileType().equals("f")) {
                            devopsBucketFile.setGlobalId(tTask.getStoryId());
                            devopsBucketFile.setSource("2");
                            result.add(devopsBucketFile);
                        }
                    }
                }
                String replace = bucketFile.getBucketFileUrl().replace(take.getSystemId() + "/", "");
                minioUtil.copyObject(take.getSystemId().toLowerCase(), replace, "devops-" + take.getPlaneId(), replace);

            } else {
                String url = bucketFile.getBucketFileUrl().replaceAll(take.getSystemId(), take.getPlaneId());
                devopsBucketFile = new DevopsBucketFile();
                devopsBucketFile.setBucketFileId(bucketFile.getBucketFileId());
                devopsBucketFile.setBucketId(take.getPlaneId());
                devopsBucketFile.setBucketFilePid(bucketFile.getBucketFilePid());
                devopsBucketFile.setBucketFileType(bucketFile.getBucketFileType());
                devopsBucketFile.setBucketFileName(bucketFile.getBucketFileName());
                devopsBucketFile.setBucketFileCode(PinyinUtil.getPinyin(bucketFile.getBucketFileName()));
                devopsBucketFile.setBucketFileUrl(bucketFile.getBucketFileUrl().replace(take.getSystemId(), take.getPlaneId()));
                devopsBucketFile.setBucketFileRemark(bucketFile.getBucketFileRemark());
                devopsBucketFile.setGlobalId("");
                devopsBucketFile.setSource("3");
                devopsBucketFile.setFileType(bucketFile.getFileType());
                devopsBucketFile.setCreateUser("devops");
                devopsBucketFile.setCreateUserName("devops");
                devopsBucketFile.setUpdateTime(LocalDateTime.now());
                devopsBucketFile.setCreateTime(LocalDateTime.now());
                devopsBucketFile.setUpdateUser("devops");
                devopsBucketFile.setUpdateUserName("devops");
                devopsBucketFile.setIsValid("Y");
                devopsBucketFile.setLesseeId("devops");
                //交付物
                String replace = bucketFile.getBucketFileUrl().replace(take.getSystemId() + "/", "");
                minioUtil.copyObject(take.getSystemId().toLowerCase(), replace, "devops-" + take.getPlaneId(), replace);
                result.add(devopsBucketFile);

            }
        }
        System.out.println(result);
        devopsBucketFileService.saveBatch(result);
    }

    public void moveProductBucketFile(TProductPlane take) {

        String projectId = take.getSystemId();
        HashMap<String, Object> paramMap8 = new HashMap<>();
        paramMap8.put("projectId", projectId);
        String bucketFiles = HttpUtil.post(URLConfig.CONSUMER_URL + "queryBucketFileByProjectId", paramMap8);
        List<TBucketFile> bucketFileList = JSON.parseArray(bucketFiles, TBucketFile.class);
        if (bucketFileList.isEmpty()) {
            return;
        }
        if (bucketFileList.size() == 1) {
            TBucketFile bucketFile = bucketFileList.get(0);
            if (bucketFile.getBucketFileUrl().contains("里程碑")) {
                return;
            }
        }


        //查询出所有的 epic
        HashMap<String, Object> paramMap = new HashMap<>();
        paramMap.put("systemId", projectId);
        String epics = HttpUtil.post(URLConfig.CONSUMER_URL + "queryEpicBySystemId", paramMap);
        List<TEpic> epicList = JSON.parseArray(epics, TEpic.class);


        //查询出所有的 story
        HashMap<String, Object> paramMap2 = new HashMap<>();
        paramMap2.put("systemId", projectId);
        String storys = HttpUtil.post(URLConfig.CONSUMER_URL + "queryStoryBySystemId", paramMap2);
        List<TStory> storyList = JSON.parseArray(storys, TStory.class);

        //查询出所有的 task 任务
        HashMap<String, Object> paramMap3 = new HashMap<>();
        paramMap3.put("systemId", projectId);
        String tasks = HttpUtil.post(URLConfig.CONSUMER_URL + "queryTaskBySystemId", paramMap3);
        List<TTask> tTasks = JSON.parseArray(tasks, TTask.class);
        //处理里程碑内的数据
        List<TBucketFile> bucketFileList1 = bucketFileList.stream().filter(e -> e.getBucketFileUrl().contains("里程碑") && !e.getBucketFileCode().equals("lichengbei")).collect(Collectors.toList());
        Map<String, String> collect = bucketFileList.stream().collect(Collectors.toMap(TBucketFile::getBucketFileUrl, TBucketFile::getBucketFileId));
        DevopsBucketFile devopsBucketFile = null;
        List<DevopsBucketFile> result = new ArrayList<>();
        for (TBucketFile bucketFile : bucketFileList1) {
            String pid = "";
            String fileName = "";
            if (bucketFile.getBucketFileType().equals("f")) {
                int i = bucketFile.getBucketFileUrl().lastIndexOf("/");
                String substring = bucketFile.getBucketFileUrl().substring(0, i + 1);
                if (substring.equals("/" + take.getSystemId() + "/" + "里程碑" + "/")) {
                    pid = "milestone-" + take.getProductPlaneId();
                } else {
                    pid = collect.get(substring);
                }
                int i1 = bucketFile.getBucketFileUrl().lastIndexOf("/");
                //文件名称
                fileName = bucketFile.getBucketFileUrl().substring(i1 + 1);
            }
            if (bucketFile.getBucketFileType().equals("d")) {
                String url = bucketFile.getBucketFileUrl().substring(0, bucketFile.getBucketFileUrl().length() - 1);
                int i = url.lastIndexOf("/");
                fileName = url.substring(i + 1);
                String prePath = url.substring(0, i + 1);
                if (prePath.equals("/" + take.getSystemId() + "/" + "里程碑" + "/")) {
                    pid = "milestone-" + take.getProductPlaneId();
                } else {
                    pid = collect.get(prePath);
                }

            }

            devopsBucketFile = new DevopsBucketFile();
            devopsBucketFile.setBucketFileId(bucketFile.getBucketFileId());
            devopsBucketFile.setBucketId(take.getProductPlaneId());
            devopsBucketFile.setBucketFilePid(pid);
            devopsBucketFile.setBucketFileType(bucketFile.getBucketFileType());
            devopsBucketFile.setBucketFileName(fileName);
            devopsBucketFile.setBucketFileCode(PinyinUtil.getPinyin(fileName));
            devopsBucketFile.setBucketFileUrl(bucketFile.getBucketFileUrl().replace(take.getSystemId(), take.getProductPlaneId()));
            devopsBucketFile.setBucketFileRemark(bucketFile.getBucketFileRemark());
            devopsBucketFile.setGlobalId("");
            devopsBucketFile.setSource("3");
            devopsBucketFile.setFileType(bucketFile.getFileType());
            devopsBucketFile.setCreateUser("devops");
            devopsBucketFile.setCreateUserName("devops");
            devopsBucketFile.setUpdateTime(LocalDateTime.now());
            devopsBucketFile.setCreateTime(LocalDateTime.now());
            devopsBucketFile.setUpdateUser("devops");
            devopsBucketFile.setUpdateUserName("devops");
            devopsBucketFile.setIsValid("Y");
            devopsBucketFile.setLesseeId("devops");

            String replace = bucketFile.getBucketFileUrl().replace(take.getSystemId() + "/", "");
//            minioUtil.copyObject(take.getSystemId(), "/里程碑/settings.xml", "devops-" + take.getPlaneId(), replace.substring(1));
            minioUtil.copyObject(take.getSystemId().toLowerCase(), replace, "devops-" + take.getProductPlaneId(), replace);
            result.add(devopsBucketFile);
        }

        for (TBucketFile bucketFile : bucketFileList) {
            if (bucketFile.getBucketFileUrl().contains("里程碑")) {
                continue;
            }
            int i = bucketFile.getBucketFileUrl().lastIndexOf("/");
            String substring2 = bucketFile.getBucketFileUrl().substring(i);
            devopsBucketFile = new DevopsBucketFile();
            devopsBucketFile.setBucketFileId(bucketFile.getBucketFileId());
            devopsBucketFile.setBucketId(take.getProductPlaneId());
            devopsBucketFile.setBucketFilePid("task-" + take.getProductPlaneId());
            devopsBucketFile.setBucketFileType(bucketFile.getBucketFileType());
            devopsBucketFile.setBucketFileName(bucketFile.getBucketFileName());
            devopsBucketFile.setBucketFileCode(bucketFile.getBucketFileCode());
            devopsBucketFile.setBucketFileUrl(bucketFile.getBucketFileUrl().replace(take.getSystemId(), take.getProductPlaneId()));
            devopsBucketFile.setBucketFileRemark(bucketFile.getBucketFileRemark());
            devopsBucketFile.setFileType(FileNameUtil.getSuffix(substring2));
            devopsBucketFile.setCreateUser("devops");
            devopsBucketFile.setCreateUserName("devops");
            devopsBucketFile.setUpdateTime(LocalDateTime.now());
            devopsBucketFile.setCreateTime(LocalDateTime.now());
            devopsBucketFile.setUpdateUser("devops");
            devopsBucketFile.setUpdateUserName("devops");
            devopsBucketFile.setIsValid("Y");
            devopsBucketFile.setLesseeId("devops");

            if (StringUtils.isNotBlank(bucketFile.getSeq())) {
                //epic附件
                for (TEpic tEpic : epicList) {
                    if (tEpic.getEpicId().equals(bucketFile.getSeq())) {
                        if (bucketFile.getBucketFileType().equals("f")) {
                            devopsBucketFile.setGlobalId(tEpic.getEpicId());
                            devopsBucketFile.setSource("2");
                            result.add(devopsBucketFile);
                        }
                    }
                }

                //story附件
                for (TStory tStory : storyList) {
                    if (tStory.getStoryId().equals(bucketFile.getSeq())) {
                        if (bucketFile.getBucketFileType().equals("f")) {
                            devopsBucketFile.setGlobalId(tStory.getStoryId());
                            devopsBucketFile.setSource("2");
                            result.add(devopsBucketFile);
                        }
                    }
                }

                //task附件
                for (TTask tTask : tTasks) {
                    if (tTask.getTaskId().equals(bucketFile.getSeq())) {
                        if (bucketFile.getBucketFileType().equals("f")) {
                            devopsBucketFile.setGlobalId(tTask.getStoryId());
                            devopsBucketFile.setSource("2");
                            result.add(devopsBucketFile);
                        }
                    }
                }
                String replace = bucketFile.getBucketFileUrl().replace(take.getSystemId() + "/", "");
                minioUtil.copyObject(take.getSystemId().toLowerCase(), replace, "devops-" + take.getProductPlaneId(), replace);
            } else {
                String url = bucketFile.getBucketFileUrl().replaceAll(take.getSystemId(), take.getProductPlaneId());
                devopsBucketFile = new DevopsBucketFile();
                devopsBucketFile.setBucketFileId(bucketFile.getBucketFileId());
                devopsBucketFile.setBucketId(take.getProductPlaneId());
                devopsBucketFile.setBucketFilePid(bucketFile.getBucketFilePid());
                devopsBucketFile.setBucketFileType(bucketFile.getBucketFileType());
                devopsBucketFile.setBucketFileName(bucketFile.getBucketFileName());
                devopsBucketFile.setBucketFileCode(PinyinUtil.getPinyin(bucketFile.getBucketFileName()));
                devopsBucketFile.setBucketFileUrl(bucketFile.getBucketFileUrl().replace(take.getSystemId(), take.getProductPlaneId()));
                devopsBucketFile.setBucketFileRemark(bucketFile.getBucketFileRemark());
                devopsBucketFile.setGlobalId("");
                devopsBucketFile.setSource("3");
                devopsBucketFile.setFileType(bucketFile.getFileType());
                devopsBucketFile.setCreateUser("devops");
                devopsBucketFile.setCreateUserName("devops");
                devopsBucketFile.setUpdateTime(LocalDateTime.now());
                devopsBucketFile.setCreateTime(LocalDateTime.now());
                devopsBucketFile.setUpdateUser("devops");
                devopsBucketFile.setUpdateUserName("devops");
                devopsBucketFile.setIsValid("Y");
                devopsBucketFile.setLesseeId("devops");
                //交付物
                String replace = bucketFile.getBucketFileUrl().replace(take.getSystemId() + "/", "");
                minioUtil.copyObject(take.getSystemId().toLowerCase(), replace, "devops-" + take.getProductPlaneId(), replace);
                result.add(devopsBucketFile);
            }
        }
        devopsBucketFileService.saveBatch(result);
    }
}
